﻿using System;
using System.IO;
using System.Linq;
using EditRegion.MVC.Repository;
using EditRegion.MVC.Repository.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EditRegion.MVC.Tests
{
    [TestClass()]
    public class XmlContentRepositoryTest
    {
        #region TestContext
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        } 
        #endregion

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void UpdateTest_With_Valid_RegionId()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "Region2";
            const string content = "New Content";
            const string expected = "New Content";
            target.Update(contentId, content);
            string actual = target.GetById(contentId);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateTest_With_InValid_RegionId()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "InvalidRegionId";
            const string content = "New Content";

            try
            {
                target.Update(contentId, content);
                Assert.IsTrue(false);
            }
            catch (Exception)
            {
                Assert.IsTrue(true);
            }
        }

        [TestMethod()]
        public void IsExistingTest_When_True()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "Region1";
            const bool expected = true;
            bool actual = target.IsExisting(contentId);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void IsExistingTest_When_False()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "InvalidRegionId";
            const bool expected = false;
            bool actual = target.IsExisting(contentId);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void GetByContentIdTest_With_Valid_RegionId()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "Region1";
            const string expected = "Region1";
            string actual = target.GetById(contentId);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void GetByContentIdTest_With_InValid_RegionId()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "InvalidRegionId";
            const string expected = null;
            string actual = target.GetById(contentId);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void GetAllTest()
        {
            IContentRepository target = CreateIContentRepository();
            var actual = target.GetAll();
            Assert.IsTrue(actual.ToList().Count > 1);
        }

        [TestMethod()]
        public void DeleteTest()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "Region3";
            target.Delete(contentId);
            string actual = target.GetById(contentId);
            Assert.IsNull(actual);
        }

        protected virtual IContentRepository CreateIContentRepository()
        {
            if (File.Exists("Content.xml")) { File.Delete("Content.xml"); }
            FileInfo fileInfo = new FileInfo(@"Content.xml");
            IContentRepository target = new XmlRepository(fileInfo);
            target.Add("Region1", "Region1");
            target.Add("Region2", "Region2");
            target.Add("Region3", "Region3");
            return target;
        }

        [TestMethod()]
        public void AddTest()
        {
            IContentRepository target = CreateIContentRepository();
            const string contentId = "NewContentId";
            const string content = "Brand New Content";
            target.Add(contentId, content);
            var actual = target.GetById(contentId);
            Assert.AreEqual(content, actual);
        }
    }
}
